package com.manger.chrome;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeOptions;

import javax.swing.*;
import java.io.File;
import java.util.List;

public class DriverMangerBak {

    public static WebDriver driver;

    public static void init(){
        try{
            //"D:\Program Files\Chrome\App\chrome.exe"
            // --no-first-run --disable-component-update --disable-crash-reporter
            // --disable-breakpad --disable-logging --no-report-upload --disable-background-updates --disable-features=PrintCompositorLPAC --force-renderer-accessibil
            String driverPath = System.getProperty("user.dir") + "/../chromedriver-win64/chromedriver.exe";
            System.out.println(driverPath);
            System.setProperty("webdriver.chrome.driver", driverPath);
            File file = new File(driverPath);
            System.out.println(file.getCanonicalPath());
            ChromeOptions options = new ChromeOptions();
            String tempDirPath = System.getProperty("user.dir") + "/20251016150822" ;
            options.addArguments("--user-data-dir=" + tempDirPath);
            // 禁止显示"Chrome 正受到自动测试软件的控制。"
            options.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"});
            driver = new org.openqa.selenium.chrome.ChromeDriver(options);
            driver.get("https://zxfw.court.gov.cn/zxfw/#/pagesGrxx/pc/login/index");

            WebElement element =  driver.findElement(By.className("fd-system-user-login"));
            List<WebElement> elements = element.findElements(By.className("fd-sub-tab"));
            if(elements.size()>1){
                elements.get(1).click();
            }
            Runtime.getRuntime().addShutdownHook(new Thread() {
                @Override
                public void run() {
                    driver.quit();
                    System.out.println("Shutting down");
                }
            });
        }catch (Exception e){
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "访问网络异常！", "警告", JOptionPane.WARNING_MESSAGE);
            try {
                driver.quit();
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }
    }
}
